﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCadDll;
using MyIni;
using System.IO;

namespace CsharpCad2
{
    public partial class Form1 : Form
    {
        MyCadDll.MyCadDll mycad=new MyCadDll.MyCadDll();
        IniFile myini=new IniFile(Directory.GetCurrentDirectory()+"\\mycad.ini");
        public Form1()
        {
            InitializeComponent();
        }

        private void StartCad_Click(object sender, EventArgs e)
        {
            mycad.StartCad();
        }

        private void ReadCadToIni_Click(object sender, EventArgs e)
        {
            mycad.ReadSelectSetToIni();
        }

        //private void ReadIniToCad_Click(object sender, EventArgs e)
        //{

        //}
        //用于写传感器板的数据
        //section为ini文件中对应的结名称 tss1 tss2对应配线端子的正负部分名称name为设备名称
        private void WriteCGQ(string section,string tss1,string tss2,string tname)
        {
            string name, zuhe, style;
            string ss1, ss2;
            int x1 = 18, x2 = 38, y1 = 13;//定义表格递进大小
            int[] y2 = { 0, 6, 6, 6, 7, 7, 8, 6, 6, 6, 8, 6, 7, 6, 6, 7 };//因为配线栏的大小不均匀，所以要分别定义宽带
            double[] p1 = { 500, 500, 0 };
            double[] p2 = { 518, 500, 0 };
            double[] p3 = { 500, 500, 0 };
            int count = 0;
            int count1 = 0;
            int sum = myini.GetInt16(section,"sum",0);
            ss1 = tss1;
            ss2 = tss2;
            for (int i = 1; i <= sum; i++)
            {
                name = myini.GetString(section, i.ToString(), "", 0);
                zuhe = myini.GetString(section, i.ToString(), "", 1);
                style = myini.GetString(section, i.ToString(), "", 2);
                mycad.WriteToCad(p1, name + tname, 2);
                p2[1] = p2[1] - y2[count1++];
                mycad.WriteToCad(p2, zuhe + tss1, 2);
                p2[1] = p2[1] - y2[count1++];
                mycad.WriteToCad(p2, zuhe + tss2, 2);
                p1[1] = p1[1] - y1;
                if (++count % 8 == 0)
                {
                    p1[0] = p1[0] + x2;
                    p1[1] = 500;
                    p2[0] = p2[0] + x2;
                    p2[1] = 500;
                    count1 = 0;
                }
            }

        }
//写集中式采集机的数据 配置文件1个记录，对应1个采集量
        private void WriteCjjName(string section ,string tname)
        {
            int x1 = 56;
            int[] y1={0,7,7,5,7,6,7,6,7,7,6,6,7,7,6,7,5,7,6,7,6,7,6,7};
            double[] p1 = { 200, 800, 0 };
            double[] p2 = { 200, 800, 0 };
            double[] p3 = { 500, 500, 0 };
            int count = 0;
            int count1 = 0;
            string name;
            int sum = myini.GetInt16(section, "sum", 0);
            for (int i = 1; i <= sum; i++)
            {
                
                name = myini.GetString(section, i.ToString(), "", 0);

                //count++;
                if(i%2==1)
                {    
                    p1[0]=p2[0];
                }
                else
                { 
                    p1[0]=p2[0]+x1;                    
                }
                mycad.WriteToCad(p1, name + tname, 2);
               
             
                if (i % 48 == 0)
                {

                    p2[0] = p2[0] + 2 * x1;
                    p1[1] = 800;
                    p1[0] = p2[0];
                    count = 0;
                }
                if (i % 2 == 0)
                    p1[1] = p1[1] - y1[++count];
               // mycad.WriteToCad(p1, name + tname, 2);
           
            }


        }
 //写集中式采集机的数据 配置文件1个记录，对应2个采集量 如灯丝电流 X 对应X-1DJ X-2DJ .道岔表示 1个道岔对应定位反位表示电压
        private void WriteCjjName(string section, string tname1, string tname2)
        {
            int x1 = 56;
            int[] y1 = { 0, 7, 7, 5, 7, 6, 7, 6, 7, 7, 6, 6, 7, 7, 6, 7, 5, 7, 6, 7, 6, 7, 6, 7 };
            double[] p1 = { 200, 800, 0 };
            double[] p2 = { 200, 800, 0 };
            double[] p3 = { 500, 500, 0 };
            int count = 0;
            int count1 = 0;
            string name;
            int sum = myini.GetInt16(section, "sum", 0);
            for (int i = 1; i <= sum; i++)
            {

                name = myini.GetString(section, i.ToString(), "", 0);

                mycad.WriteToCad(p1, name + tname1, 2);
                p1[0] = p2[0] + x1;
                mycad.WriteToCad(p1, name + tname2, 2);
                p1[0] = p2[0];
                if (i % 24 == 0)
                {

                    p2[0] = p2[0] + 2 * x1;
                    p1[1] = 800;
                    p1[0] = p2[0];
                    count = -1;
                }
             
                    p1[1] = p1[1] - y1[++count];
                // mycad.WriteToCad(p1, name + tname, 2);

            }
        }
        //写集中式采集机的数据,包括写入名字和端子号
        private void WriteCjjName(string section, string tname,int tzuhe,int tss1)
        {
            int[] x1 = {0,20,38,56};
            int[] y1 = { 0, 7, 7, 5, 7, 6, 7, 6, 7, 7, 6, 6, 7, 7, 6, 7, 5, 7, 6, 7, 6, 7, 6, 7 };
            double[] p1 = { 200, 800, 0 };
            double[] p2 = { 200, 800, 0 };
            double[] p3 = { 500, 500, 0 };
            int count = 0;
            int count1 = 0;
            string name;
            string zuhe;
            string ss1;
            int sum = myini.GetInt16(section, "sum", 0);
            for (int i = 1; i <= sum; i++)
            {
                p1[1] = p1[1] - y1[(++count/2)*(count%2)];
                name = myini.GetString(section, i.ToString(), "", 0);
                zuhe = myini.GetString(section, i.ToString(), "", 1);
                ss1 = myini.GetString(section, i.ToString(), "", 2);
                p1[0]=p2[0]+x1[(i-1)%2*3];
                mycad.WriteToCad(p1, name + tname, 2);
                p1[0] = p2[0] + x1[(i-1) % 2 +1];
                mycad.WriteToCad(p1, zuhe +ss1, 2);
                p1[0] = p2[0];
                if (i % 24 == 0)
                {

                    p2[0] = p2[0] + 2 * 56;
                    p1[1] = 800;
                    p1[0] = p2[0];
                    count = 0;
                }

                
                // mycad.WriteToCad(p1, name + tname, 2);

            }
        }
        //用于写灯丝电流，因为灯丝电流一个名称对应1DJ和2DJ所以单独处理比较简单些，做成统一模块比较复杂
        private void ReadDsdlToCad_Click(object sender, EventArgs e)
        {
            string name,zuhe,style;
            string jz1dj, jz2dj, cz1dj, cz2dj,qj1dj,qj2dj;
            jz1dj = myini.GetString("灯丝电流", "进站1DJ", "");
            jz2dj = myini.GetString("灯丝电流", "进站2DJ", "");
            cz1dj = myini.GetString("灯丝电流", "出站1DJ", "");
            cz2dj = myini.GetString("灯丝电流", "出站2DJ", "");
            qj1dj = myini.GetString("灯丝电流", "区间1DJ", "");
            qj2dj = myini.GetString("灯丝电流", "区间2DJ", "");
            int x1 = 18,x2=38, y1 = 13;//定义表格递进大小
            int[] y2 = {0,6,6,6,7,7,8,6,6,6,8,6,7,6,6,7 };//因为配线栏的大小不均匀，所以要分别定义宽带
            int count = 0;
            int count1 = 0;
            int sum = myini.GetInt16("灯丝电流", "sum",0);
            double[] p1 = { 500, 500, 0 };
            double[] p2={518,500,0};
            double[] p3={500,500,0};
            for (int i = 1; i <= sum; i++)
            {
                 name = myini.GetString("灯丝电流", i.ToString(), "error", 0);
                 zuhe = myini.GetString("灯丝电流", i.ToString(), "error", 1);
                 style = myini.GetString("灯丝电流", i.ToString(), "error", 2);

                 mycad.WriteToCad(p1, name + "-DJ电流", 2);
                 if (style.CompareTo("1") == 0)
                 {
                     p2[1] = p2[1] - y2[count1++];
                     mycad.WriteToCad(p2, zuhe + "-DJ6", 2);
                     p2[1] = p2[1] - y2[count1++];
                     mycad.WriteToCad(p2, zuhe + "-"+jz1dj, 2); 
                 }
                 if (style.CompareTo("2") == 0)
                 {
                     p2[1] = p2[1] - y2[count1++];
                     mycad.WriteToCad(p2, zuhe + "-DJ6", 2);
                     p2[1] = p2[1] - y2[count1++];
                     mycad.WriteToCad(p2, zuhe + "-" + cz1dj, 2); 
                 }
                 if (style.CompareTo("3") == 0)
                 {
                     p2[1] = p2[1] - y2[count1++];
                     mycad.WriteToCad(p2, zuhe + "-DJ2", 2);
                     p2[1] = p2[1] - y2[count1++];
                     mycad.WriteToCad(p2, zuhe + "-" + qj1dj, 2); 
                 }
                 p1[1] = p1[1] - y1;
                 if (++count % 8 == 0)
                 {
                     p1[0] = p1[0] + x2;
                     p1[1] = 500;
                     p2[0] = p2[0] + x2;
                     p2[1] = 500;
                     count1 = 0;
                 }
                // p2[1] = p2[1] - y2[count1++];
                
                 mycad.WriteToCad(p1, name + "-2DJ电流", 2);
                 if (style.CompareTo("1") == 0)
                 {
                     p2[1] = p2[1] - y2[count1++];
                     mycad.WriteToCad(p2, zuhe + "-2DJ6", 2);
                     p2[1] = p2[1] - y2[count1++];
                     mycad.WriteToCad(p2, zuhe + "-" + jz2dj, 2);
                 }
                 if (style.CompareTo("2") == 0)
                 {
                     p2[1] = p2[1] - y2[count1++];
                     mycad.WriteToCad(p2, zuhe + "-2DJ6", 2);
                     p2[1] = p2[1] - y2[count1++];
                     mycad.WriteToCad(p2, zuhe + "-" + cz2dj, 2);
                 }
                 if (style.CompareTo("3") == 0)
                 {
                     p2[1] = p2[1] - y2[count1++];
                     mycad.WriteToCad(p2, zuhe + "-2DJ2", 2);
                     p2[1] = p2[1] - y2[count1++];
                     mycad.WriteToCad(p2, zuhe + "-" + qj2dj, 2);
                 }
                    p1[1] = p1[1] - y1;
                    if (++count % 8 == 0)
                    {
                        p1[0] = p1[0] +  x2;
                        p1[1] = 500;
                        p2[0] = p2[0] +  x2;
                        p2[1] = 500;
                        count1 = 0;
                    }
                    //p2[1] = p2[1] - y2[count1++];
                   
            }
            WriteCjjName("灯丝电流", "-DJ电流", "-2DJ电流");
        }

        //private void button1_Click(object sender, EventArgs e)
        //{
        //    //WriteCGQ("灯丝电流", "1", "2", "aaa");  
        //    WriteCjjName("灯丝电流", "-DJ电流","-2DJ电流");
        //}

        private void ReadCadToJy_Click(object sender, EventArgs e)
        {
            mycad.ReadSelectSetToIni("jy");
        }

        private void ReadJyToCad_Click(object sender, EventArgs e)
        {
            int x = 25;
            int[] y = {0,10,12,15,12,12,13,13,11,14,14,11,12,13,11,14 };
            double[] p1 = { 200, 800, 0 };
            double[] p2 = { 200, 800, 0 };
            string name1,name2;
            int count=0;
            int sum = myini.GetInt16("jy", "sum", 0);
            for (int i = 1; i <= sum; i++)
            {
                p1[1] = p1[1] - y[(i-1) % 16];
                name1 = myini.GetString("jy", i.ToString(),"error", 0);
                mycad.WriteToCad(p1, name1, 2);
                name1 = myini.GetString("jy", i.ToString(),"error" ,1);
                p1[0] = p1[0] + x;
                mycad.WriteToCad(p1, name1, 2);
                p1[0] = p2[0];
                if (i % 16 == 0)
                {
                    p2[0] = p2[0] + 2 * x;
                    p1[0] = p2[0] ;
                    p1[1] = p2[1];
                }

            }
        }

        //为了写um71 电码化的各种模拟量，提出公共部分，而引出的函数
        //section:ini节 devicename:设备名称补充部分 tzuhe:组合在ini中的行的第几个位置  tss1 tss2：为配线端子在ini中的第几个位置
        private void um71mnltocad(string section,string devicename,int tname ,int tzuhe,int tss1,int tss2)
        {
            string name, zuhe, style;
            string ss1, ss2;
            int x1 = 18, x2 = 38, y1 = 13;//定义表格递进大小
            int[] y2 = { 0, 6, 6, 6, 7, 7, 8, 6, 6, 6, 8, 6, 7, 6, 6, 7 };//因为配线栏的大小不均匀，所以要分别定义宽带
            double[] p1 = { 500, 500, 0 };
            double[] p2 = { 518, 500, 0 };
            double[] p3 = { 500, 500, 0 };
            int count = 0;
            int count1 = 0;
            int sum = myini.GetInt16(section, "sum", 0);

            for (int i = 1; i <= sum; i++)
            {
                name = myini.GetString(section, i.ToString(), "", tname);
                zuhe = myini.GetString(section, i.ToString(), "", tzuhe);
                ss1 = myini.GetString(section, i.ToString(), "", tss1);
                ss2 = myini.GetString(section, i.ToString(), "", tss2);
                mycad.WriteToCad(p1, name + devicename, 2);
                p2[1] = p2[1] - y2[count1++];
                mycad.WriteToCad(p2, zuhe + ss1, 2);
                p2[1] = p2[1] - y2[count1++];
                mycad.WriteToCad(p2, zuhe + ss2, 2);
                p1[1] = p1[1] - y1;
                if (++count % 8 == 0)
                {
                    p1[0] = p1[0] + x2;
                    p1[1] = 500;
                    p2[0] = p2[0] + x2;
                    p2[1] = 500;
                    count1 = 0;
                }
            }
        }
        private void ReadGongChuToCad_Click(object sender, EventArgs e)
        {
            um71mnltocad("um71区间", "功出电压",0, 1,4, 5);
            WriteCjjName("um71区间", "功出电压");
        }

        private void ReadGcDlToCad_Click(object sender, EventArgs e)
        {
            um71mnltocad("um71区间", "发送电流",0, 2, 8, 9);
            WriteCjjName("um71区间", "发送电流");
        }

        private void ReadGuiRuToCad_Click(object sender, EventArgs e)
        {
            um71mnltocad("um71区间", "轨入电压",0, 1, 6, 7);
            WriteCjjName("um71区间", "轨入电压");
        }

        private void ReadXianRuToCad_Click(object sender, EventArgs e)
        {
            um71mnltocad("um71区间", "限入电压",0, 1, 14, 14);
            WriteCjjName("um71区间", "限入电压");
        }

        private void ReadFsdlToCad_Click(object sender, EventArgs e)
        {
            um71mnltocad("um71区间", "发送电缆侧",0, 3, 10, 11);
            WriteCjjName("um71区间", "发送电缆侧");
        }

        private void ReadJsdlToCad_Click(object sender, EventArgs e)
        {
            um71mnltocad("um71区间", "接收电缆侧",0, 3, 12, 13);
            WriteCjjName("um71区间", "接收电缆侧");
        }
        
       
        private void ReadDmDyToCad_Click(object sender, EventArgs e)
        {
            um71mnltocad("电码化", "电压", 0, 1, 5, 6);
            WriteCjjName("电码化", "电压");
            
        }

        private void RDmDlToCad_Click(object sender, EventArgs e)
        {
            um71mnltocad("电码化", "电流", 0, 2, 3, 4);
            WriteCjjName("电码化", "电流");
        }

        private void ReadFBJToCad_Click(object sender, EventArgs e)
        {
            WriteCjjName("FBJ", "FBJ", 1, 2);
        }

        private void ReadIniToCadMNL_Click(object sender, EventArgs e)
        {

        }
    }
}
